/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlykho;

import java.awt.Color;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author Admin
 */
public class QuanLyHangHoa extends javax.swing.JPanel {

    /**
     * Creates new form QuanLyHangHoa
     */
    static DefaultMutableTreeNode root ;
   static DefaultTreeModel model;
    static DefaultTreeModel modelTree;
    public QuanLyHangHoa() {
        try {
            initComponents();
            
            Ketnoi a = new Ketnoi();
            Connection con = a.connect();
            root = new DefaultMutableTreeNode("Quản Lý Hàng Hóa");
            model = new   DefaultTreeModel(root) ;
             modelTree = (DefaultTreeModel)tree.getModel();
             modelTree.setRoot(root);
            PreparedStatement pst = con.prepareCall("Select * from loaihang");
            ResultSet rs = pst.executeQuery();
            String str = null;
            String ten = null;
            while (rs.next())
        {
            str = rs.getString(2);
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(str);
            ten =rs.getString(1);
            PreparedStatement pst1 = con.prepareCall("Select * from hanghoa where maloaihh = ? ");
            pst1.setString(1, ten);     
            ResultSet rs1 = pst1.executeQuery();
            String str1 = null;
            while(rs1.next())
            {
             str1=rs1.getString(2);
             DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(str1);
             //System.out.println(str1);
             node.add(node1);
            }
           // System.out.println("alo");
            root.add(node);
        }
        
        tree.expandRow(0);
        tree.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {
                TreePath num = e.getPath();
                int i = num.getPathCount();
                if(i==1)
                {
                root.removeAllChildren();
                loadRoot();
                modelTree.setRoot(root);
                p1.removeAll();
                QuanLyHang b = new QuanLyHang();
                b.setSize(450, 430);
                b.setLocation(0, 0);
                b.setVisible(true);
                b.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
                p1.add(b);
                p1.updateUI();
                p1.setVisible(true);
                }
                if(i==2)
                {
                DefaultMutableTreeNode key = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                //System.out.println(key.toString());
                String st = key.toString();
                p1.removeAll();
                ThongTinHangHoa b = new ThongTinHangHoa(st);
                b.setSize(450, 430);
                b.setLocation(0, 0);
                b.setVisible(true);
                //b.setBorder(BorderFactory.createLineBorder(Color.red));
                p1.add(b);
                p1.updateUI();
                p1.setVisible(true);   
                }
                if(i==3)
                {
                DefaultMutableTreeNode key = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                String st = key.toString();
                p1.removeAll();
                TTCTHang b = new TTCTHang(st);
                b.setSize(450, 430);
                b.setLocation(0, 0);
                b.setVisible(true);
                b.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
                p1.add(b);
                p1.updateUI();
                p1.setVisible(true);
                }

            }
        });
        } catch (SQLException ex) {
            Logger.getLogger(QuanLyHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
             
    }
    public void loadRoot()
    {
        try {
            Ketnoi a = new Ketnoi();
             Connection con = a.connect();
//           // root = new DefaultMutableTreeNode("Quản Lý Hàng Hóa");
//             model = new   DefaultTreeModel(root) ;
//             DefaultTreeModel modelTree = (DefaultTreeModel)tree.getModel();
//            modelTree.setRoot(root);

    PreparedStatement pst = con.prepareCall("Select * from loaihang");
    ResultSet rs = pst.executeQuery();
    String str = null;
    String ten = null;
    while (rs.next())
{
    try {
        str = rs.getString(2);
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(str);
        ten =rs.getString(1);
        PreparedStatement pst1 = con.prepareCall("Select * from hanghoa where maloaihh = ? ");
        pst1.setString(1, ten);     
        ResultSet rs1 = pst1.executeQuery();
        String str1 = null;
        while(rs1.next())
        {
         str1=rs1.getString(2);
         DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(str1);
         System.out.println(node1.toString());
         node.add(node1);
            
        }
       // System.out.println("alo");
        root.add(node);
        
    } catch (SQLException ex) {
        Logger.getLogger(QuanLyHangHoa.class.getName()).log(Level.SEVERE, null, ex);
    }
}       } catch (SQLException ex) {
            Logger.getLogger(QuanLyHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

  

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();
        p1 = new javax.swing.JPanel();

        tree.setBorder(new javax.swing.border.MatteBorder(null));
        tree.setFont(new java.awt.Font("Tahoma", 1, 12));
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        tree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane1.setViewportView(tree);

        p1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout p1Layout = new javax.swing.GroupLayout(p1);
        p1.setLayout(p1Layout);
        p1Layout.setHorizontalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 499, Short.MAX_VALUE)
        );
        p1Layout.setVerticalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 441, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(p1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(118, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                .addGap(43, 43, 43))
            .addGroup(layout.createSequentialGroup()
                .addComponent(p1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel p1;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables
}
